package com.casework

class LegalCaseController {
    static scaffold = true

    def deleteOffer ={
        def offer = Offer.get(params.id)
        def legalCase = LegalCase.get(params.caseId)

        if(legalCase?.removeFromOffers(offer).save()){
            render 'offer removed'
        } else{
            render status: 400, text: "Unable to remove offer"
        }
    }

    def deleteNace = {
        def nace = Nace.get(params.id)
        def legalCase = LegalCase.get(params.caseId)

        def join = LegalCaseNaceFullDescription.findByDescriptionAndLegalCase(nace, legalCase)
        if(LegalCaseNaceFullDescription.findByDescriptionAndLegalCase(nace, legalCase)){
            join.delete()
            render 'nace removed'
        } else{
            render status: 400, text: "Unable to remove nace"
        }
    }

    def deleteMeasure = {
        def measure = Measure.get(params.id)
        def legalCase = LegalCase.get(params.caseId)

        if(legalCase?.removeFromMeasures(measure).save()){
            measure.delete()
            render 'measure removed'
        } else{
            render status: 400, text: "Unable to remove measure"
        }
    }

    def deleteFirm = {
        def firm = Firm.get(params.id)
        def legalCase = LegalCase.get(params.caseId)

        def join = LegalCaseFirm.findByFirmAndLegalCase(firm, legalCase)
        if(join){
            join.delete()
            render 'firm removed'
        } else {
            render status: 400, text: "Unable to remove firm"
        }

    }
    
    def deleteAttachedFile = {
        def file = AttachedFile.get(params.id)
        def legalCase = LegalCase.get(params.caseId)

        if(legalCase?.removeFromFiles(file).save()){
            file.delete()
            render 'file removed'
        } else{
            render status: 400, text: "Unable to remove file"
        }
    }

    def show = {
        def legalCaseInstance = LegalCase.get(params.id)
        
        if (!legalCaseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'legalCase.label', default: 'LegalCase'), params.id])
            redirect(action: "list")
        }
        else {
            def descriptions = LegalCaseNaceFullDescription.findAllByLegalCase(legalCaseInstance).collect { it.description } as Set
            def firms = LegalCaseFirm.findAllByLegalCase(legalCaseInstance).collect { it.firm } as Set

            [legalCaseInstance: legalCaseInstance, descriptions: descriptions.sort{a,b -> a.code <=> b.code}, firms: firms.sort{a,b -> a.name <=> b.name}]
        }
    }

    def addFirm = {
        if(!params.caseId || !params.firmId){
            render status: 503, text: 'Case ID and firm ID must be passed in'
        }

        def legalCase = LegalCase.get(params.caseId)
        def firm = Firm.get(params.firmId)
        LegalCaseFirm.create(legalCase, firm, true)

        render template: 'link', model: [item: firm, controllerName: 'firm']
    }

    def addNace = {
        if(!params.caseId || !params.naceId){
            render status: 503, text: 'Case ID and nace ID must be passed in'
        }

        def legalCase = LegalCase.get(params.caseId)
        def nace = Nace.get(params.naceId)
        LegalCaseNaceFullDescription.create(legalCase, nace, true)

        render template: 'link', model: [item: nace, controllerName: 'nace']
    }

    def lastUpdated = {
        def legalCaseInstanceList = LegalCase.listOrderByLastUpdated(max:5, order:"desc")
        [legalCaseInstanceList: legalCaseInstanceList]
    }
}
